package com.netbao.fin.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.netbao.fin.mapper.InvoiceMapper;
import com.netbao.fin.mapper.SubtotalMapper;
import com.netbao.fin.model.Invoice;
import com.netbao.fin.model.Subtotal;
import com.netbao.fin.service.JieService;
import com.netbao.fin.vomapper.JieVoMapper;
import com.netbao.model.PageBean;
import com.netbao.model.PageResult;
import com.netbao.model.Result;
import com.netbao.sys.utils.ShiroUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author suqk
 * @create 2019-11-06 14:49
 */
@Service
public class JieServiceImpl implements JieService {
    @Resource
    private JieVoMapper jieVoMapper;
    @Resource
    private InvoiceMapper invoiceMapper;
    @Resource
    private SubtotalMapper subtotalMapper;

    @Override
    public PageResult listAll(PageBean pageBean, String name, String year, String mouth, String day) {
        Integer eid= ShiroUtils.getUserEntity().getEid();
        if (!StringUtils.isEmpty(name)) {
            PageHelper.startPage(pageBean.getPage(), pageBean.getRows());
            PageInfo<Subtotal> userPageInfo = new PageInfo<Subtotal>(jieVoMapper.mo(pageBean, name,(long)eid));
            return new PageResult(userPageInfo.getTotal(), userPageInfo.getList());
        }
        if(!StringUtils.isEmpty(year)) {
            String date = null;
            if (mouth == null || mouth == "") {
                date = year;
            } else if (day == null || day == "") {
                date = year + "-" + mouth;
            } else {
                date = year + "-" + mouth + "-" + day;
            }
            PageHelper.startPage(pageBean.getPage(), pageBean.getRows());
            PageInfo<Subtotal> userPageInfo = null;
            if (day == "" || day == null) {
                userPageInfo = new PageInfo<Subtotal>(jieVoMapper.cha(date, pageBean,(long)eid));
            } else {
                userPageInfo = new PageInfo<Subtotal>(jieVoMapper.cha1(date, pageBean,(long)eid));
            }
            return new PageResult(userPageInfo.getTotal(), userPageInfo.getList());
        }
        PageHelper.startPage(pageBean.getPage(), pageBean.getRows());
        PageInfo<Subtotal> userPageInfo = new PageInfo<Subtotal>(jieVoMapper.listAll(pageBean,(long)eid));
        return new PageResult(userPageInfo.getTotal(), userPageInfo.getList());
    }

    @Override
    public Result dan(Long id, Long payment) throws ParseException {
        Integer eid= ShiroUtils.getUserEntity().getEid();
        Invoice invoice = new Invoice();
        String piaonum = null;
        for (int i = 0; i < 10; i++) {
            piaonum = String.valueOf((Math.random()*9+1)*10000);
        }
        List<Invoice> list = null;
        if (jieVoMapper.list((long)eid) != null) {
            list = jieVoMapper.list((long)eid);
            for (int i = 0; i < list.size(); i++) {
                if (piaonum == list.get(i).getCno()) {
                    dan(id, payment);
                }
            }
        }

        Date date = null;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        date = dateFormat.parse(dateFormat.format(new Date()));
        invoice.setCno(piaonum);
        invoice.setTid(payment);
        invoice.setSid(String.valueOf(id));
        invoice.setCreatetime(date);
        invoice.setEid((long)eid);
        invoiceMapper.insertSelective(invoice);
        Subtotal subtotal = new Subtotal();
        subtotal.setId(id);
        subtotal.setPiaostatus((long) 1);
        subtotal.setPiaonum(piaonum);
        subtotalMapper.updateByPrimaryKeySelective(subtotal);
        return Result.ok();
    }
}
